package cn.asura.server;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * TCP: 服务端
 */
public class MyServer {
    public static void main(String[] args) throws Exception {

        // 约定端口9999
        int port = 9999;

        // 创建服务器对象
        try (ServerSocket ss = new ServerSocket(port);  // 获取套接字对象
             Socket accept = ss.accept();               // 获取输入流
             InputStream is = accept.getInputStream()) {// 读取数据

            // 创建一个字节数组
            byte[] bytes = new byte[1024];
            @SuppressWarnings("unused")
            int read = is.read(bytes);

            // 打印接受的数据
            String s = new String(bytes, StandardCharsets.UTF_8);
            System.out.println("接受的数据为：" + s);

            // 服务端响应客户端信息
            OutputStream os = accept.getOutputStream();
            String msg = "谢谢回顾";
            os.write(msg.getBytes());


        }
    }
}
